<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no"/>
    <title>添加或编辑界面</title>
    <link rel="stylesheet" type="text/css" href="${s.base}/lib/layui/css/layui.css"/>
    <link rel="stylesheet" type="text/css" href="${s.base}/css/base/index.css"/>
    <link rel="stylesheet" type="text/css" href="${s.base}/css/base/theme/theme.css"/>
</head>
<body>
<div class="wrap-container clearfix">
    <div class="column-content-detail">
        <form class="layui-form" action="">
        <div class="tab-step-ctn mart20">
            <div class="tab-step-line checked">
                <span class="circle">1</span>
                <div class="tab-step-name">基础信息填写</div>
                <i class="iconfont">&#xe688;</i>
            </div>
            <div class="tab-step-line">
                <span class="circle">2</span>
                <div class="tab-step-name">添加考生</div>
                <i class="iconfont">&#xe688;</i>
            </div>
            <div class="tab-step-line">
                <span class="circle">3</span>
                <div class="tab-step-name">考场分配</div>
                <i class="iconfont">&#xe688;</i>
            </div>
            <div class="tab-step-line">
                <span class="circle">4</span>
                <div class="tab-step-name">监、巡考老师设置</div>
                <i class="iconfont">&#xe688;</i>
            </div>
            <div class="tab-step-line">
                <span class="circle">5</span>
                <div class="tab-step-name">发布考试通知</div>
                <i class="iconfont">&#xe688;</i>
            </div>
        </div>
        <div class="kw-mian-ctn">
            <table class="wxyy-item-table" cellspacing="0" cellpadding="0" width="100%">
                <tr>
                    <td><span class="red">*</span>考务名称</td>
                    <td class="wxyy-td-tetxleft" colspan="3"><input type="text" class="wxyy-tab-input" value="" placeholder="请输入考务名称" id="title" ></td>

                </tr>
                <tr>
                    <td><span class="red">*</span>考试类型</td>
                    <td class="wxyy-td-tetxleft" colspan="3" id="ksType">
                        <input type="radio" name="ksType" value="1" title="新生考试" lay-filter="ksType">
                        <input type="radio" name="ksType" value="2" title="月度考试" lay-filter="ksType">
                        <input type="radio" name="ksType" value="3" title="期中考试" lay-filter="ksType">
                        <input type="radio" name="ksType" value="4" title="期末考试" lay-filter="ksType">
                    </td>
                </tr>
                <tr>
                    <td><span class="red">*</span>开始时间</td>
                    <td class="wxyy-td-tetxleft" colspan="1"><input  type="text" name="startTime" id="startTime" value="" placeholder=" 请选择开始时间" autocomplete="off" class="layui-input wxyy-tab-input"></td>

                    <td><span class="red">*</span>结束时间 </td>
                    <td class="wxyy-td-tetxleft" colspan="1"><input type="text" name="endTime" id="endTime"  value="" placeholder=" 请选择结束时间" autocomplete="off" class="layui-input wxyy-tab-input"></td>




                </tr>
            </table>
            <div class="wxyy-tab-add">
                <table class="wxyy-table mart10" cellspacing="0" cellpadding="0" width="100%" id="kmTable">
                    <thead>
                        <tr>
                            <th width="156">考试科目</th>
                            <th>考试时间</th>
                        </tr>
                    </thead>
                    <tbody>
                    </tbody>
                </table>
                <div class="tab-add-line" id="add"><i class="iconfont">&#xe611;</i><input type="hidden" id="courseName"/>添加考试科目</div>
            </div>
            <table class="wxyy-item-table mart10" cellspacing="0" cellpadding="0" width="100%">
                <tr>
                    <td><span class="red">*</span>考试地点</td>
                    <td class="wxyy-td-tetxleft" colspan="3"><input type="text" id="address"  class="wxyy-tab-input" placeholder="请输入考试地点" value=""></td>

                </tr>
                <tr>
                    <td>考试须知</td>
                    <td>
                        <div class="kw-notcie-list" id="remark">
                        1、考生凭《准考证》在每科考前十五分钟进入考场，凭《准考证》对号入座，将《准考证》放在课桌左上角。<br>
                        2、考生进入考场，不得携带计算器、通讯工具和书籍。<br>
                        3、迟到30分钟不得进入考场、考试60分钟后才准交卷离场。<br>
                        4、除在试卷上应填写姓名和准考证号码外，不得作其他任何标记。<br>
                        5、学生必须严格遵守考场纪律，对于违反纪律或作弊者，分别给予批评教育、试卷作废、取消考试资格等处理。<br>
                        6、考试终了时间一到，考生立即停止答卷，不得将试卷和草稿纸带走，违者按零分处理。
                        </div>
                    </td>
                </tr>
            </table>
            <div class="layui-form-item ">
                <div class="layui-input-block" style="margin-left: 0px !important;text-align: center; margin-top: 30px !important;">
                    <button class="layui-btn layui-btn-normal wxyy-tab-btn" id="nextStep">下一步</button>
                    <button type="button" id="cancel" class="layui-btn layui-btn-primary wxyy-tab-btn">取消</button>
                </div>
            </div>
        </div>
        </form>
    </div>
</div>
<script src="${s.base}/lib/layui/layui.js" type="text/javascript" charset="utf-8"></script>
<script src="${s.base}/js/common.js" type="text/javascript" charset="utf-8"></script>
<script>

    var courseNum = 0;
    var applyId='${applyId}';
    var baseutil = null;
    var remarkStr = "";
    var ksId = '${ksId}';
    var menuId='${parentMenuId}';
    var name ="0"

    layui.use(['form', 'jquery','table', 'fileUtil', 'dialog', 'layer','page', 'layedit', 'baseutil','laydate','upload'], function () {
        var $ = layui.jquery,
            form = layui.form,
            baseutil = layui.baseutil,
            laydate = layui.laydate,
            page = layui.page,
            dialog = layui.dialog,
            table = layui.table;
//            baseutil.layerDateTime("startTime","yyyy-MM-dd HH:mm");
//            baseutil.layerDateTime("endTime","yyyy-MM-dd HH:mm");

       //开始时间设置
        laydate.render({
            elem: '#startTime'
            ,type: 'datetime'
            ,format: 'yyyy-MM-dd HH:mm'
        });

        //结束时间设置
        laydate.render({
            elem: '#endTime'
            ,type: 'datetime'
            ,format: 'yyyy-MM-dd HH:mm'
        });

       //考试类型监听
        form.on('radio(ksType)', function (data) {
            name = "1";
        });

        //添加考试科目
        $("#add").click(function () {
            var String =  $("#courseName").val();
            dialog.popUpScSubjectApplyMultSelect(String,String,"科目选择",String,function (id,name) {
                $("#courseName").val(name);
            });
        });

        //获取已经设置好的基本信息
        postData();

        function postData() {
            if(ksId.length>0){
                baseutil.post('/work/kwmanagement/kwmanagement/addOrUpdateStop', {"step": "1","id":ksId}, function (result) {
                    if (0 == result.code) {
                        baseutil.post('/work/kwmanagement/kwmanagement/findOneVo', {"id":ksId}, function (result) {
                            if (0 == result.code) {
                                $("#title").val(result.data.title);

                                $("#startTime").val(result.data.startTime);
                                $("#endTime").val(result.data.endTime);
                                name ="1";
                                $("input:radio[name='ksType'][value= '"+result.data.ksType+"']").attr("checked", true);
                                form.render();

                                var kmInfo = result.data.kmMessageVOList;
                                var courseName = [];
                                for (var i = 0; i < kmInfo.length; i++) {
                                    courseName.push(kmInfo[i].kmmc);
                                }
                                $("#courseName").val(courseName.join(","));
                                initKmInfo(kmInfo);
                                $("#address").val(result.data.address);
                                remarkStr = result.data.remark;
                            }
                        });

                    }
                });


            }else {
                var remark = '<p style="color:#666666;font-family:&quot;font-size:14px;background-color:#FFFFFF;">';
                remark += '<span style="font-size:14px;">1、考生凭《准考证》在每科考前十五分钟进入考场，凭《准考证》对号入座，将《准考证》放在课桌左上角。</span>';
                remark += '</p>';
                remark += '<p style="color:#666666;font-family:&quot;font-size:14px;background-color:#FFFFFF;">';
                remark += '<span style="font-size:14px;">2、考生进入考场，不得携带计算器、通讯工具和书籍。</span>';
                remark += '</p>';
                remark += '<p style="color:#666666;font-family:&quot;font-size:14px;background-color:#FFFFFF;">';
                remark += '<span style="font-size:14px;">3、迟到30分钟不得进入考场、考试60分钟后才准交卷离场。</span>';
                remark += '</p>';
                remark += '<p style="color:#666666;font-family:&quot;font-size:14px;background-color:#FFFFFF;">';
                remark += '<span style="font-size:14px;">4、除在试卷上应填写姓名和准考证号码外，不得作其他任何标记。</span>';
                remark += '</p>';
                remark += '<p style="color:#666666;font-family:&quot;font-size:14px;background-color:#FFFFFF;">';
                remark += '<span style="font-size:14px;">5、学生必须严格遵守考场纪律，对于违反纪律或作弊者，分别给予批评教育、试卷作废、取消考试资格等处理。</span>';
                remark += '</p>';
                remark += '<p style="color:#666666;font-family:&quot;font-size:14px;background-color:#FFFFFF;">';
                remark += '<span style="font-size:14px;">6、考试终了时间一到，考生立即停止答卷，不得将试卷和草稿纸带走，违者按零分处理。</span>';
                remark += '</p>';
                remarkStr = remark;
            }
        }
        //取消
        $("#cancel").on("click",function () {
            parent.tabDelete('kwmangementEdit');
        });


        //监听提交 下一步
        $("#nextStep").click(function () {
            var data = {};
            data.ksType = $(":radio[name='ksType']:checked").val();
            data.id = '${kwManagement.id}';
            data.applyId = applyId;
            data.startTime = $("#startTime").val();
            data.endTime = $("#endTime").val();
            data.address = $("#address").val();
            data.remark = remarkStr;
            data.state = "0";
            data.step = "1";
            data.title = $("#title").val();
            var time = convertStringToDate( data.endTime) - convertStringToDate(data.startTime);

            if( data.title ==""){
                dialog.msg("请输入考务名称");
                return false;
            }

            if(name=="0"){
                dialog.msg("请选择开始类型");
                return false;
            }

            if( data.startTime ==""){
                dialog.msg("请选择开始时间");
                return false;
            }
            if( data.endTime ==""){
                dialog.msg("请选择结束时间");
                return false;
            }
            if (time < 0) {
                dialog.msg("结束时间应大于开始时间");
                return false;
            }

            if( data.address ==""){
                dialog.msg("请选择考试地点");
                return false;
            }

            //科目成绩提交
            var km = [];
            var tr = $("#kmTable tbody tr");
            if (tr.length > 0) {
                for (var i = 0; i < tr.length; i++) {
                    var tName = tr[i].childNodes[0].innerHTML;
                    var tBeginTime = tr[i].childNodes[1].childNodes[0].childNodes[0].value;
                    if (tBeginTime == "" || tBeginTime == null) {
                        dialog.msg("请填写" + tName + "考试开始时间");
                        return false;
                    }
                    var tEndTimenew = "endTime_" + i;
                    var tEndTime = $("#"+tEndTimenew).val();
                    if (tEndTime == "" || tEndTime == null) {
                        dialog.msg("请填写" + tName + "考试结束时间");
                        return false;
                    }
                    //每一段时间的判断情况
                    var timeSub = convertStringToDate(tEndTime) - convertStringToDate(tBeginTime);
                    if(timeSub<0){
                        dialog.msg(tName+"结束时间应大于开始时间");
                        return false;
                    }
                    var timeStart = convertStringToDate(tBeginTime) - convertStringToDate($("#startTime").val());
                    if(timeStart<0){
                        dialog.msg(tName+":考试开始时间应大于科目考试开始时间");
                        return false;
                    }
                    var timeEnd = convertStringToDate(tEndTime) - convertStringToDate($("#endTime").val());
                    if(timeEnd>0){
                        dialog.msg(tName+":考试结束时间应大于科目考试结束时间");
                        return false;
                    }



                    km.push({
                        kmmc: tName,
                        beginTime: tBeginTime + ":00",
                        endTime: tEndTime + ":00"
                    });
                    data.kmMessageJson = JSON.stringify(km);
                }
        }else {
            dialog.msg("请添加考试科目");
            return false;
        }
            baseutil.post("/work/kwmanagement/kwmanagement/addOrUpdate", data, function (req) {
                if(0==req.code){
                    if(isBlank(ksId)){
                        ksId = req.data;
                    }
                    parent.tabRefresh(menuId);
                    page.edit("getNewEdit","/work/kwmanagement/kwstudentmessage/getNewEdit?applyId="+applyId+"&ksId="+ksId+"&parentMenuId="+menuId,"添加学生");

                    //刷新上一个界面的操作
                    parent.tabDelete('kwmangementEdit');
                }else {
                    dialog.msg("保存失败");
                }

            });
            return false;
        });
    });

    //下拉时间选择器的弹出框
    function cytSelectMoreCourseCallBack(names) {
        debugger;
        courseNum = 0;
        var name = names.split(",");
        var kmInfo = [];
        for (var i = 0; i < name.length; i++) {
            kmInfo.push({kmmc: name[i], beginTime: "", endTime: ""});
        }
        var tr = $("#kmTable tbody tr");
        if (tr.length > 0) {
            for (var i = 0; i < tr.length; i++) {
                var tName = tr[i].childNodes[0].innerHTML;
                var tBeginTime = tr[i].childNodes[1].childNodes[0].childNodes[0].value;
                //var tEndTime = tr[i].childNodes[1].childNodes[1].childNodes[0].value;
                var tEndTimenew = "endTime_" + i;
                var tEndTime = $("#"+tEndTimenew).val();
                for (var j = 0; j < kmInfo.length; j++) {
                    if (tName == kmInfo[j].kmmc) {
                        kmInfo[j].beginTime = tBeginTime;
                        kmInfo[j].endTime = tEndTime;
                        break;
                    }
                }
            }
        }
        initKmInfo(kmInfo);
    }



    //设置考试科目详情
    function initKmInfo(kmInfo) {
        debugger;
        var str = '';
        for (var i = 0; i < kmInfo.length; i++) {
            str += '<tr>';
            str += '<td>' + kmInfo[i].kmmc + '</td>';
            str += '<td>';
            str+= '<div class="cyt-data-line2">';
            str+= '<input type="text" lay-verify="date" id="startTime_' + courseNum + '" placeholder="请选择开始时间" value="' + kmInfo[i].beginTime + '">';
            str+= '<span>至</span>';
            str+='<input type="text" ay-verify="date" id="endTime_' + courseNum + '" placeholder="请选择结束时间" value="' + kmInfo[i].endTime + '">';
            str+='</div>';
            str += '</td>';
            str += '</tr>';
            courseNum++;
        }
        $("#kmTable tbody").html(str);
            for (var i = 0; i < courseNum; i++) {
                selectDataByIds("startTime_" + i, "yyyy-MM-dd HH:mm");
                selectDataByIds("endTime_" + i, "yyyy-MM-dd HH:mm");
            }

    }

    //设置科目选择时间的固定格式
    function selectDataByIds (objId, format, range) {
        if (format == "" || format == null) {
            format = "yyyy-MM-dd HH:mm";
        }
        if (range == true) {
            range = true;
        } else {
            range = false;
        }
        layui.use('laydate', function () {
            var laydate = layui.laydate;
            laydate.render({
                elem: '#' + objId //指定元素
                , format: format
                ,type: 'datetime'
                , range: range
                ,trigger: 'click'//解决日期闪没问题
                , done: function (value, date) {
                    //通过回调函数设置值
                    if (typeof CytCallBackDate == 'function') {
                        CytCallBackDate(objId, value, date);
                    }
                }
            });
        });
    }
    function convertStringToDate(dateString) {
        var arr = dateString.split(" ");
        var date = arr[0].split("-");
        var date = new Date(date[0], date[1] - 1, date[2]);
        return date;
    }


</script>
</body>
</html>